<template>
  <u-popup v-if="show" v-model="show" mode="center" :mask="true" @close="bindClose()">
    <div  class="rst-content">
      <div class="rst-content-body">
        <div class="rst-user">
          <image v-if="userMessage.referenceImage" class="user-img" :src="userMessage.referenceImage" mode="aspectFit"></image>
          <div class="time-txt">
            <div class="time">{{ userMessage.time }}</div>
            <u-read-more>
              <div class="txt">{{ userMessage.prompt }}</div>
            </u-read-more>
          </div>
        </div>

        <div class="rst-ai">
          <div class="ai-tip">以下是为你生成的图片：</div>
          <div class="rst-img">
            <image class="rst-img-item" :src="userMessage.multipleImages" mode="aspectFit"></image>
          </div>
        </div>
      </div>
    </div>
  </u-popup>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getPoints, aiGetById } from '@/api/designApi';

const userMessage = ref<any>({
  referenceImage: '',
  prompt: '',
  time: '',
  multipleImages: '',
});

const text = ref('');
const show = ref(false);

const open = ({ id, time, referenceImage, prompt }) => {
  show.value = true;
  userMessage.value = {
    id,
    referenceImage,
    prompt,
    time,
  };
  getDetailById(id);
};

onMounted(() => {
  checkPoints();
});

const checkPoints = () => {
  // 获取用户身上AI总次数
  getPoints().then((res) => {
    // console.log('getPoints', res);
  });
};

const getDetailById = (id) => {
  uni.showLoading({ title: '图片生成中', mask: true });
  aiGetById({ id }).then((res) => {
    // console.log('res', res);
    if (res.data.jobType === 1 || res.data.jobType === 0) {
      setTimeout(() => {
        getDetailById(id);
      }, 2000);
    } else if (res.data.jobType === 2) {
      uni.hideLoading();
      userMessage.value = res.data;
    } else {
      uni.hideLoading();
      uni.showToast({
        title: '生成失败, 请稍后再试',
        icon: 'none',
      });
    }
  });
};

defineExpose({
  open,
});

const bindClose = () => {
  show.value = false;
};
</script>

<style scoped lang="scss">
.rst-content {
  .rst-content-body {
    .rst-user {
      padding: 20rpx 32rpx;
      display: flex;
      gap: 24rpx;
      background: #ffffff;
      .user-img {
        flex-shrink: 0;
        width: 108rpx;
        height: 108rpx;
        border-radius: 8rpx 8rpx 8rpx 8rpx;
      }
      .time-txt {
        display: flex;
        flex-direction: column;
        gap: 16rpx;
        .time {
          font-weight: 400;
          font-size: 24rpx;
          color: #6f6f6f;
          line-height: 32rpx;
        }
        .text {
          font-weight: 400;
          font-size: 24rpx;
          color: #222222;
          line-height: 34rpx;
        }
      }
    }
    .rst-ai {
      margin: 10rpx 32rpx;
      background: #f3f7f8;
      border-radius: 8rpx 8rpx 8rpx 8rpx;
      padding: 16rpx;
      .ai-tip {
        font-weight: 400;
        font-size: 24rpx;
        color: #222222;
        line-height: 32rpx;
        margin: 16rpx 0 20rpx;
      }
      .rst-img {
        width: 500rpx;
        margin: auto;
      }
    }
  }
}
</style>
